<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="场馆名称" prop="venueName">
        <el-input
          v-model="queryParams.venueName"
          placeholder="请输入场馆名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
          @change="handleQuery"
        >
          <el-option label="正常" value="0" />
          <el-option label="冻结" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新建场馆</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column type="index" />
      <el-table-column label="场馆名称" prop="venueName" min-width="200" />
      <el-table-column label="会员数" prop="memberNumber" min-width="80" />
      <el-table-column label="今日流水" prop="todayFlow" min-width="100" />
      <el-table-column label="场馆负责人" prop="managerName" min-width="120" />
      <el-table-column label="联系方式" prop="managerTelno" min-width="120" />
      <el-table-column label="关联场馆" prop="relateVenueNames" min-width="150" />
      <el-table-column label="租赁费用" prop="rentPrice" min-width="150" />
      <el-table-column label="有效期" min-width="200">
        <template #default="scope">
          <span>{{ scope.row.validityStart.substr(0,10) }} ~ {{ scope.row.validityEnd.substr(0,10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="statusStr" min-width="100" />
      <el-table-column label="操作人" prop="updateBy" min-width="150" />
      <el-table-column label="操作时间" prop="updateTime" min-width="180" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding" width="230">
        <template #default="scope">
          <el-button type="text" @click="updateRow(scope.row)">编辑</el-button>
          <el-button type="text" @click="removeRow(scope.row)">删除</el-button>
          <el-button type="text" @click="viewRow(scope.row)">详情</el-button>
          <el-button type="text" @click="changeStatus(scope.row)">{{ scope.row.status==0?'冻结':'解冻' }}</el-button>
          <el-button type="text" @click="">延期</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <!-- 新建项目/编辑项目 -->
    <dialogModifyVenue :props="dialogModifyVenueProps" :venue-price-list="venuePriceList" @submitForm="resetQuery" />
  </div>
</template>

<script setup name="List">
import { getCurrentInstance, onMounted } from '@vue/runtime-core'
import { ref } from 'vue'
import dialogModifyVenue from './components/dialog/modifyVenue'
import { changeVenueStatus, delVenue, getVenueList } from '../../../api/oam/venue'
import { getBaseVenuePriceList } from '../../../api/oam/basic'


const { proxy } = getCurrentInstance()

const showSearch = ref(true)
const total = ref(0)
const loading = ref(true)
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  venueName: '',
  status: ''
})
const tableData = ref([])
const venuePriceList = ref([])

const dialogModifyVenuePropsBase = {
  open: false,
  title: '',
  isView: false,
  data: null
}
const dialogModifyVenueProps = ref({
  ...dialogModifyVenuePropsBase
})


// 获取列表
function getList() {
  loading.value = true
  getVenueList(queryParams.value).then((res) => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleAdd() {
  _.assign(dialogModifyVenueProps.value, dialogModifyVenuePropsBase, {
    open: true,
    title: '新建场馆'
  })
}

function updateRow(row) {
  _.assign(dialogModifyVenueProps.value, dialogModifyVenuePropsBase, {
    open: true,
    title: '编辑场馆',
    data: { ...row }
  })
}

function removeRow(row) {
  proxy.$modal
    .confirm(`是否确认删除场馆 ${row.venueName}`)
    .then(() => {
      return delVenue(row.id)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
}

function viewRow(row) {
  _.assign(dialogModifyVenueProps.value, dialogModifyVenuePropsBase, {
    open: true,
    title: '查看场馆',
    isView: true,
    data: { ...row }
  })
}

function changeStatus(row) {
  proxy.$modal
    .confirm(`确定操作？`)
    .then(() => {
      return changeVenueStatus(row)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('操作成功')
    })
}

onMounted(() => {
  getList()

  getBaseVenuePriceList().then((res) => {
    venuePriceList.value = res.rows
  })
})

</script>

<style scoped lang="scss">

</style>
